package com.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.sfzy.entity.InfoNotice;
import com.sfzy.service.InfoNoticeService;
import com.sfzy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("infoNotice")
public class InfoNoticeController {
    @Autowired
    private InfoNoticeService infoNoticeService;
    //增
    @PostMapping
    public R addInfoNotice(@RequestBody InfoNotice infoNotice) {
        int i = infoNoticeService.addInfoNotice(infoNotice);
        if(i==0){
            return R.fail("增加失败！");
        }
        return R.ok("增加成功！");
    }
    //删
    @DeleteMapping("{id}")
    public R deleteInfoNotice(@PathVariable("id") int id) {
        int i = infoNoticeService.deleteInfoNotice(id);
        if(i==0){
            return R.fail("删除失败！");
        }
        return R.ok("删除成功！");
    }
    //改
    @PutMapping
    public R updateInfoNotice(@RequestBody InfoNotice infoNotice) {
        int i = infoNoticeService.updateInfoNotice(infoNotice);
        if(i==0){
            return R.fail("修改失败！");
        }
        return R.ok("修改成功！");
    }
    //根据id查
    @GetMapping("{id}")
    public R getInfoNoticeById(@PathVariable("id") int id) {
        InfoNotice infoNotice = infoNoticeService.getInfoNoticeById(id);
        return R.ok(infoNotice);
    }
    //分页查
    @GetMapping("page")
    public R getAllInfoNotice(InfoNotice infoNotice,
                              @RequestParam(required = false,defaultValue = "1") int pageNum,
                              @RequestParam(required = false,defaultValue = "5") int pageSize) {
        PageInfo<InfoNotice> allInfoNotice = infoNoticeService.getAllInfoNotice(infoNotice, pageNum, pageSize);
        return R.ok(allInfoNotice);
    }
}
